package com.xw.mailservice.model;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述:分页模型
 * @author 杨建全
 * @date 2017年6月6日 下午2:13:55
 * @param <T> 存放的数据类型
 */
public class Page<T> {

	private Integer current;
	private Integer size = 10;
	private Integer total;
	private Integer pages;
	private List<T> records = new ArrayList<T>();
	private Integer offsetBegin;
	private Integer offsetEnd;

	public Page(int current, int size) {
        this.current = current;
        this.size = size;
        if (current > 0) {
        	this.offsetBegin = (current - 1) * size;
        }else{
        	this.offsetBegin = 0;
        }
        this.offsetEnd = offsetBegin+size;
    }
	
	public Integer getCurrent() {
		return current;
	}

	public void setCurrent(Integer current) {
		this.current = current;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public List<T> getRecords() {
		return records;
	}

	public void setRecords(List<T> records) {
		this.records = records;
	}

	public Integer getPages() {
		if (this.size == 0) {
			return 0;
		}
		this.pages = this.total / this.size;
		if (this.total % this.size != 0) {
			this.pages++;
		}
		return this.pages;
	}

	public void setPages(Integer pages) {
		this.pages = pages;
	}
	
	public boolean hasPrevious() {
        return this.current > 1;
    }

    public boolean hasNext() {
        return this.current < this.pages;
    }
    
    protected static int offsetCurrent(int current, int size) {
        if (current > 0) {
            return (current - 1) * size;
        }
        return 0;
    }

	public Integer getOffsetBegin() {
		return offsetBegin;
	}

	public void setOffsetBegin(Integer offsetBegin) {
		this.offsetBegin = offsetBegin;
	}

	public Integer getOffsetEnd() {
		return offsetEnd;
	}

	public void setOffsetEnd(Integer offsetEnd) {
		this.offsetEnd = offsetEnd;
	}

	@Override
	public String toString() {
		return "Page [current=" + current + ", size=" + size + ", total=" + total + ", pages=" + pages + "]";
	}
    
}
